package com.woniuxy.driverschool.controller;/* *
@author author
@create 2021-09-29 20:21
*/

import com.woniuxy.driverschool.entity.Period;
import com.woniuxy.driverschool.entity.PeriodDetail;
import com.woniuxy.driverschool.entity.ResponseResult;
import com.woniuxy.driverschool.service.PeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PeriodController {

    @Autowired
    PeriodService periodService;

    @GetMapping("/period")
    ResponseResult<List<Period>> getAllPeriods(){
        ResponseResult<List<Period>> responseResult=new ResponseResult<List<Period>>();
        responseResult.setData(periodService.getAllPeriods());
        responseResult.setMessage("获取全部时段成功");
        responseResult.setState(200);
        return responseResult;
    }

    @GetMapping("/period/{periodId}")
    ResponseResult<Period> getPeriodById(@PathVariable("periodId") int periodId){
        ResponseResult<Period> responseResult=new ResponseResult<Period>();
        responseResult.setData(periodService.getPeriodById(periodId));
        responseResult.setMessage("获取全部时段成功");
        responseResult.setState(200);
        return responseResult;
    }

    @PostMapping("/period")
    ResponseResult addPeriod(Period period){
       periodService.addPeriod(period);
       ResponseResult responseResult=new ResponseResult();
       responseResult.setMessage("添加成功");
       responseResult.setState(200);
       return responseResult;
    }

    @PutMapping("/period")
    ResponseResult editPeriod(@RequestBody Period period){
        for (PeriodDetail periodDetail:period.getPeriodDetails()) {
            periodDetail.setTimeFrom(periodDetail.getTimes().get(0));
            periodDetail.setTimeTo(periodDetail.getTimes().get(1));
            periodDetail.setPeriodId(period.getPeriodId());
        }
        periodService.modifyPeriod(period);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMessage("添加成功");
        responseResult.setState(200);
        return responseResult;
    }

}
